package pe.com.trestperu.common.service.impl;

import java.io.Serializable;
import java.sql.Connection;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import pe.com.trestperu.common.exception.CoreDBException;
import pe.com.trestperu.common.exception.CoreServiceException;
import pe.com.trestperu.common.model.dao.GenericDAO;
import pe.com.trestperu.common.service.GenericService;

/**
 *
 * @author Telly Tataje
 *
 */
public class GenericServiceImpl implements GenericService {
	
	protected Log log = LogFactory.getLog(this.getClass());
	
	private GenericDAO genericDAO;
	
	public void setGenericDAO(GenericDAO genericDAO){
		this.genericDAO = genericDAO;
	}
	
	public GenericDAO getDAO(){
		return genericDAO;
	}
	
	public Connection getConnection() throws CoreServiceException{
		try {
			return getDAO().getConnection();
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}
	
	public void delete(Object persistentInstance) throws CoreServiceException{
		try{
			getDAO().delete(persistentInstance);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public void deleteById(Class<?> clazz, Serializable id) throws CoreServiceException{
		try{
			getDAO().deleteById(clazz, id);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public Object findById(Class<?> clazz, Serializable id) throws CoreServiceException{
		try{
			return getDAO().findById(clazz,id);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public List<?> findByObject(Object object) throws CoreServiceException{
		try{
			return getDAO().findByObject(object);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public Date getCurrentDate() throws CoreServiceException{
		try{
			return getDAO().getCurrentDate();
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public List<?> getFindCriteria(Object object) throws CoreServiceException {
		try{
			return getDAO().getFindCriteria(object);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public List<?> getFindCriteria(Object object, String orderBy) throws CoreServiceException{
		try{
			return getDAO().getFindCriteria(object, orderBy);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public List<?> getFindCriteria(Object object, int maxResults) throws CoreServiceException{
		try{
			return getDAO().getFindCriteria(object, maxResults);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public List<?> getFindCriteria(Object object, int maxResults, String orderBy) throws CoreServiceException{
		try{
			return getDAO().getFindCriteria(object, maxResults, orderBy);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public List<?> getFindCriteria(Object object, String orderBy, String restringe) throws CoreServiceException{
		try{
			return getDAO().getFindCriteria(object, orderBy, restringe);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public String getIdFieldName(Class<?> clazz) throws CoreServiceException{
		try{
			return getDAO().getIdFieldName(clazz);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public void save(Object object) throws CoreServiceException{
		try{
			getDAO().save(object);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public void saveCollection(Collection<?> collection) throws CoreServiceException{
		try{
			getDAO().saveCollection(collection);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}
	
	public List<?> mapListar(String namedQuery, Object object) throws CoreServiceException{
		try{
			return getDAO().mapListar(namedQuery, object);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public List<?> listar(String namedQuery, Object object) throws CoreServiceException{
		try{
			return getDAO().listar(namedQuery, object);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}
	
	public List<?> listar(String namedQuery, Object object, Class<?> clazz) throws CoreServiceException{
		try{
			return getDAO().listar(namedQuery, object, clazz);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public Object executeProcedure(String namedQuery, Object object) throws CoreServiceException{
		try{
			return getDAO().executeProcedure(namedQuery, object);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}
	
	public Object executeProcedure(String namedQuery, Object object, Class<?> clazz) throws CoreServiceException{
		try{
			return getDAO().executeProcedure(namedQuery, object, clazz);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public Object executeVoidProcedure(String namedQuery, Object object) throws CoreServiceException{
		try{
			return getDAO().executeVoidProcedure(namedQuery, object);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public List<?> mapListarQuery(String sqlQuery, Object object) throws CoreServiceException{
		try{
			return getDAO().mapListarQuery(sqlQuery, object);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public List<?> listarQuery(String sqlQuery, Object object) throws CoreServiceException{
		try{
			return getDAO().listarQuery(sqlQuery, object);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}
	
	public List<?> listarQuery(String sqlQuery, Object object, Class<?> clazz) throws CoreServiceException{
		try{
			return getDAO().listarQuery(sqlQuery, object, clazz);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public Object executeQuery(String sqlQuery, Object object) throws CoreServiceException{
		try{
			return getDAO().executeQuery(sqlQuery, object);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}
	
	public Object executeQuery(String sqlQuery, Object object, Class<?> clazz) throws CoreServiceException{
		try{
			return getDAO().executeQuery(sqlQuery, object, clazz);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public Object executeVoidQuery(String sqlQuery, Object object) throws CoreServiceException{
		try{
			return getDAO().executeVoidQuery(sqlQuery, object);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public void initialize(Object object) throws CoreServiceException {
		try{
			getDAO().initialize(object);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}
	}

	public void initializeCollection(Collection<?> collection)
			throws CoreServiceException {
		try{
			getDAO().initializeCollection(collection);
		} catch (CoreDBException e) {
			log.error(e);
			throw new CoreServiceException(e);
		}		
	}

	public Map<String, Object> executeProcedureOut(String namedQuery, Map<String, Object> mapIn, Map<String, Object> mapOut) throws CoreServiceException{
		try{
			return getDAO().executeProcedureOut(namedQuery, mapIn, mapOut);
		} catch (CoreDBException e){
			log.error(e);
			throw new CoreServiceException(e);
		}
	}
}